package com.nova.health.ble.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;

import com.nova.health.ble.device.BaseBleDevice;

public class MyBleBroadcastReceiver extends BroadcastReceiver {
    private static final String TAG = "MyBroadcastReceiver";
    private Update update;
    private Context mContext;

    public MyBleBroadcastReceiver(Context context, Update u) {
        update = u;
        mContext = context;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        String data = intent.getStringExtra("data");
        if (update != null) {
            update.update(data);
        }
        Log.d(TAG, "action:" + action + ",data:" + data);
    }

    public interface Update {
        void update(String hexStr);
    }

    public void registerReceiver(MyBleBroadcastReceiver myBroadcastReceiver) {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BaseBleDevice.writeSuccessAction);
        intentFilter.addAction(BaseBleDevice.writeFailedAction);
        intentFilter.addAction(BaseBleDevice.notifyAction);
        mContext.registerReceiver(myBroadcastReceiver, intentFilter);
    }
}
